package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.entity.AnalysisCatalog;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.service.IAnalysisCatalogService;
import com.bridgeintelligent.tag.utils.PasswordHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "画像分析目录管理")
@RestController
@AllArgsConstructor
@RequestMapping("/api/alcatalog")
@Slf4j
public class AnalysisCatalogController {

    private IAnalysisCatalogService analysisCatalogService;


    @ApiOperation(value = "获取画像分析目录列表", notes = "画像分析目录管理")
    @GetMapping("list")
    public Result list() {
       try {
           List<AnalysisCatalog> list = analysisCatalogService.findList();
           return new Result(0,"查询成功",list);
       }catch (Exception e){
           log.error("获取列表失败::"+e);
           return new Result(-1,"查询失败");
       }
    }

    @ApiOperation(value = "增加某个目录", notes = "画像分析目录管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "analysisCatalog", value = "目录信息")
    })
    @PostMapping("/add")
    public Result add(@RequestBody AnalysisCatalog analysisCatalog) {

        try {
            //校验特殊字符
           if ( PasswordHelper.checkSpecial(analysisCatalog.getAlName())){
               return new Result(-1,"名字含有特殊字符！");
           }

            //校验目录等级
            if (!StringUtils.isEmpty(analysisCatalog.getPid()) && StringUtils.equals("-",analysisCatalog.getPid())){
                analysisCatalog.setAlLevel("1");
            }else{
                String dbLevel = analysisCatalogService.checkLevel(analysisCatalog.getPid());
                if (StringUtils.isEmpty(dbLevel)){
                    return new Result(-1,"父目录不存在！");
                }
                int level = Integer.parseInt(dbLevel);
                if (level >= 2){
                    return new Result(-1,"最多支持二级目录！");
                }
                analysisCatalog.setAlLevel("2");
            }

            boolean success = analysisCatalogService.checkName(analysisCatalog.getAlName(), analysisCatalog.getPid());
            if (success){
                return new Result(-1,"同级目录下名字已存在！");
            }

            analysisCatalogService.addCatalog(analysisCatalog);
            return new Result(0,"新增成功");
        }catch (Exception e){
            log.error("新增失败::"+e);
            return new Result(-1,"新增失败");
        }
    }

    @ApiOperation(value = "修改某个目录", notes = "画像分析目录管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "analysisCatalog", value = "目录信息")
    })
    @PostMapping("/update")
    public Result update(@RequestBody AnalysisCatalog analysisCatalog) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(analysisCatalog.getAlName())){
                return new Result(-1,"名字含有特殊字符！");
            }
            analysisCatalogService.updateCatalog(analysisCatalog);
            return new Result(0,"修改成功");
        }catch (Exception e){
            log.error("修改失败::"+e);
            return new Result(-1,"修改失败");
        }
    }

    @ApiOperation(value = "删除某个目录", notes = "画像分析目录管理")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", name = "id", value = "目录ID", required = true)})
    @DeleteMapping("/del")
    public Result del(String id) {
        try {
            return analysisCatalogService.delCatalog(id);
        }catch (Exception e){
            log.error("删除失败::"+e);
            return new Result(-1,"删除失败");
        }
    }
}
